import { FormSchema } from '@/components/Form/src/types'
import { reactive, ref, vModelText } from 'vue'
import { DepartmentItem } from '@/api/department/types'
import { getTreeDepartmentsApi } from '@/api/department'
import { useUserStoreWithOut } from '@/store/modules/user'
import { GetWorkTypesApi, getProblemTypesApi } from '@/api/role'
const userStore = useUserStoreWithOut()
console.log('route-roleId------', userStore.userInfo?.roleId)
const roleGuid = userStore.userInfo?.roleId
const depGuid = userStore.userInfo?.depGuid
export const rowStyleFC = ({ row }: any) => {
  return {
    color: row.workTypeColor
  }
}
const currentDay = new Date()
function getFormattedDate(date) {
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  return `${year}-${month}-${day}`
}

const fifteenDaysAgo = new Date()
fifteenDaysAgo.setDate(fifteenDaysAgo.getDate() - 30)
// const v = ref([
//   new Date(getFormattedDate(fifteenDaysAgo) + ' 00:00:00'),
//   new Date(getFormattedDate(currentDay) + ' 23:59:59')
// ])
const v = ref([
  getFormattedDate(fifteenDaysAgo) + ' 00:00:00',
  getFormattedDate(currentDay) + ' 23:59:59'
])
console.log(v)
const departmentList = ref<DepartmentItem[]>([])
const fetchDepartment = async () => {
  const res = await getTreeDepartmentsApi({})
  const depArr: DepartmentItem[] = []
  res.data.map((item) => {
    depArr.push({
      ...item,
      departmentName: item.depNo + '~' + item.depName,
      children: HandleItem(item)
    })
  })
  function HandleItem(item) {
    const arr: DepartmentItem[] = []
    item.children?.map((child) => {
      child = {
        ...child,
        departmentName: child.depNo + '~' + child.depName,
        children: HandleItem(child)
      }
      arr.push(child)
    })
    return arr
  }
  console.log('-------------', res.data, depArr)
  departmentList.value = depArr
}
fetchDepartment()
export const searchSchema = reactive<FormSchema[]>([
  // roleGuid == '-1' || depGuid == null?
  {
    field: 'depGuid',
    label: '部门',
    component: 'TreeSelect',
    componentProps: {
      nodeKey: 'guid',
      props: {
        label: 'departmentName',
        value: 'guid',
        children: 'children'
      },
      placeholder: '请选择部门',
      highlightCurrent: true,
      expandOnClickNode: false,
      checkStrictly: true,
      checkOnClickNode: true,
      clearable: true,
      filterable: true,
      disable: roleGuid !== '-1'
    },
    optionApi: async () => {
      // const res = await getTreeDepartmentsApi({}) //  getDepartmentApi()
      // if (res.data) {
      //   return res.data //.list
      // } else {
      return departmentList.value || []
      // }
    }
  },
  // : {
  //     field: 'depName',
  //     label: '部门',
  //     component: 'Input',
  //     componentProps: {
  //       disabled: true,
  //       value: userStore.userInfo?.depName
  //     }
  //   },
  {
    field: 'stationName',
    label: '采集站',
    component: 'Input'
  },
  {
    field: 'userName',
    label: '用户',
    component: 'Input'
  },
  {
    field: 'deviceNo',
    label: '设备编号',
    component: 'Input'
  },
  {
    field: 'fileName',
    label: '文件名称',
    component: 'Input'
  },

  // {
  //   field: 'workType',
  //   label: '作业类型',
  //   component: 'Select',
  //   componentProps: {
  //     clearable: true,
  //     filterable: true,
  //     props: {
  //       label: 'typeName',
  //       // value: 'guid'
  //       value: 'typeName'
  //     }
  //   },
  //   optionApi: async () => {
  //     const res = await GetWorkTypesApi({ pageFlag: false })
  //     return res.data?.pageModels || []
  //   }
  // },
  {
    field: 'date',
    label: '审查时间',
    component: 'DatePicker',
    componentProps: {
      style: { width: '340px' },
      type: 'datetimerange',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      StartPlaceholder: 'Start date',
      EndPlaceholder: 'End date'
    }
  },

  {
    field: 'uploadDate',
    label: '上传时间',
    component: 'DatePicker',
    componentProps: {
      style: { width: '340px' },
      type: 'datetimerange',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      StartPlaceholder: 'Start date',
      EndPlaceholder: 'End date'
    }
  },
  {
    field: 'collectDate',
    label: '采集时间',
    component: 'DatePicker',
    componentProps: {
      style: { width: '340px' },
      type: 'datetimerange',
      StartPlaceholder: 'Start date',
      EndPlaceholder: 'End date',
      arrowControl: false,
      format: 'YYYY-MM-DD HH:mm:ss',
      valueFormat: 'YYYY-MM-DD HH:mm:ss'
    }
  },
  {
    field: 'problemType',
    label: '违章类型',
    component: 'Select',
    hidden: true,
    componentProps: {
      clearable: true,
      filterable: true,
      props: {
        label: 'typeName',
        value: 'guid'
      }
    },
    optionApi: async () => {
      const res = await getProblemTypesApi({ pageFlag: false })
      return res.data || []
    }
  },
  // {
  //   field: 'remark',
  //   label: '备注',
  //   component: 'Input'
  // },
  {
    field: 'checkUserName',
    label: '审核人',
    component: 'Input'
  }
])

export const handleParams = (params: any) => {
  const { date, collectDate, uploadDate, checkdate } = params
  if (date) {
    params.startTime = date[0]
    params.endTime = date[1]
    delete params.date
  }
  if (collectDate) {
    params.collectStartTime = collectDate[0]
    params.collectEndTime = collectDate[1]
    delete params.collectDate
  }
  if (uploadDate) {
    params.uploadStartTime = uploadDate[0]
    params.uploadEndTime = uploadDate[1]
    delete params.uploadDate
  }
  if (checkdate) {
    params.checkStartTime = checkdate[0]
    params.checkEndTime = checkdate[1]
    delete params.checkdate
  }
  return params
}
